﻿using BlawkayEngine;
using BlawkayEngine.Input;
using BlawkayEngine.ScreenSystem;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace BlawkayHockey
{
    public abstract class XboxLiveSettingsScreen : MenuScreen
    {
        public XboxLiveSettingsScreen(Game oGame, string oTitle)
            : base(oGame, oTitle)
        {
            _BackgroundMenuOption = GameInfo.Texture_Square;
            _FontMenuOption = oGame.Content.Load<SpriteFont>("Menu\\MenuOption");

            TransitionRate *= 8f;
        }

        public override void Update(ScreenManager oManager)
        {
            KeyboardInput oInput = GameInfo.KeyboardInput;

            if (oInput.JustPressed(Keys.Up))
            {
                IndexDown();
            }
            if (oInput.JustPressed(Keys.Down))
            {
                IndexUp();
            }

            if (Active)
            {
                if (oInput.JustPressed(Keys.Space))
                {
                    Select(oManager);
                }
                if (oInput.JustPressed(Keys.LeftShift, Keys.RightShift))
                {
                    Cancel(oManager);
                }
            }
        }

        public override void Draw(ScreenManager oManager, SpriteBatch oSB)
        {
            Rectangle oBackgroundRect = new Rectangle(100, 300, 400, 1);
            Vector2 oOrigin = Vector2.Zero;
            SpriteEffects oEffect = SpriteEffects.None;
            float oScale = 1.0f;
            float oRotation = 0.0f;

            int oIndex = 0;
            foreach (string oOption in Options)
            {
                Color oOptionColor = Index == oIndex ?
                    Color.Yellow : Color.White;

                Vector2 oMeasure = _FontMenuOption.MeasureString(oOption);
                oBackgroundRect.Height = (int)oMeasure.Y + 6;
                Vector2 oOptionPosition = new Vector2(oBackgroundRect.Left + 5, oBackgroundRect.Top + 3);

                oSB.Draw(_BackgroundMenuOption, oBackgroundRect, null, Color.Black * 0.25f * Transition, oRotation, oOrigin, oEffect, 0.5f);
                oSB.DrawString(_FontMenuOption, oOption, oOptionPosition, oOptionColor * Transition, oRotation, oOrigin, oScale, oEffect, 1.0f);

                oBackgroundRect.Y = oBackgroundRect.Bottom + 5;
                oIndex++;
            }
        }

        protected void GoToMenu(XboxLiveDestination oDestination, ScreenManager oManager)
        {
            _Destination = oDestination;

            switch (oDestination)
            {
                case XboxLiveDestination.XboxLive:
                    GoToHomeMenu(oManager); break;

                case XboxLiveDestination.CreateMatch:
                case XboxLiveDestination.CustomSearch:
                default: oManager.RemoveScreen(this); break;
            }
        }

        public override void ActionWhenFullyAdded(ScreenManager oManager)
        {
            ////////////
        }

        public override void ActionWhenFullyRemoved(ScreenManager oManager)
        {
            Screen oDestination = null;

            switch (_Destination)
            {
                case XboxLiveDestination.CreateMatch:
                    oDestination = new CreateMatchScreen(oManager.Game);
                    break;
                case XboxLiveDestination.CustomSearch:
                    ////////////
                    break;
                case XboxLiveDestination.CreateLobby:
                    Networking.CreateSession();
                    oDestination = new LobbyScreen(oManager.Game);
                    break;
                case XboxLiveDestination.JoinLobby:
                    Networking.JoinSession();
                    oDestination = new LobbyScreen(oManager.Game);
                    break;
                case XboxLiveDestination.XboxLive:
                    // Intentionally left blank
                    break;
                default:
                    oDestination = new MainMenuScreen(oManager.Game);
                    break;
            }

            if (oDestination != null)
                oManager.AddScreen(oDestination);
        }

        void GoToHomeMenu(ScreenManager oManager)
        {
            GameInfo.ScreenManager = ScreenManager.SendScreensToNewManager(oManager, new HomeMenuManager(oManager.Game, false));
            GameInfo.ScreenManager.AddScreen(new XboxLiveMenuScreen(oManager.Game));
        }

        protected XboxLiveDestination _Destination;
        protected Texture2D _BackgroundMenuOption;
        protected SpriteFont _FontMenuOption;

        public enum XboxLiveDestination
        {
            CreateMatch,
            CustomSearch,
            XboxLive,
            CreateLobby,
            JoinLobby
        }

    }
}
